<?php
/**
 * File:          KValidatorElementText
 * Description:   -
 * Author:        Kovac "Kovacou'" Alexandre (www.kovacou.fr)
 * Last Update:   14/04/2011
 */

   abstract class KValidator_Element_Text extends KValidatorElement {
      public function length ($min = null, $max = null) {
         if (is_numeric ($min))
            $min = (int) $min;
         
         if (is_numeric ($max))
            $max = (int) $max;
         
         if (KV_DEBUG && $min !== null && $max !== null && $min > $max)
               throw new Exception ("KValidatorElementText::length() - Vous avez défini un minimum supérieur à un maximum.");
         
         $param = $this->_parameter ("length");
         $param->min = $min;
         $param->max = $max;
         
         return $this;
      }

      
      public function char () {
      	 $args = func_get_args();
         $chars = ((func_num_args() > 1) ? ',' : '').implode(',', $args);
         
         if (KV_DEBUG && $chars === "")
            throw new Exception ("KValidatorElementText::char() - Vous devez renseigner au moins 1 caractère.");
         
         $param        = $this->_parameter ("char");
         $param->chars = KValidator::parserReplace ("regex", KValidator::parserReplace("char", $chars));
         
         return $this;         
      }
      
      
      public function nochar () {
         $chars = ((func_num_args() > 1) ? ',' : '').implode(',', func_get_args());
         
         if (KV_DEBUG && $chars === "")
            throw new Exception ("KValidatorElementText::nochar() - Vous devez renseigner au moins 1 caractère.");
         
         $param        = $this->_parameter ("nochar");
         $param->chars = KValidator::parserReplace ("regex", KValidator::parserReplace("char", $chars));
         
         return $this;         
      }
      
      
      public function in () {
         $list = func_get_args();

         if (KV_DEBUG && empty($list))
            throw new Exception ('KValidatorElementText::in () - Le tableau contenant les valeurs requises est vide !');

         $param = $this->_parameter ("in");
         $param->list = $list;

         return $this;
      }
      
      
      public function noin () {
         $list = func_get_args();

         if (KV_DEBUG && empty($list))
            throw new Exception ('KValidatorElementText::noin () - Le tableau contenant les valeurs interdites est vide !');

         $param = $this->_parameter ("noin");
         $param->list = $list;

         return $this;
      }
      
      
      public function filter ($filter = null) {
         $filter = mb_strtolower ($filter);
         
         $param = $this->_parameter ("filter");
         $param->filter = $filter;
         
         return $this;
      }
      
      
      public function regex ($pattern = null, $return = true, $error = null) {
         if (true === KV_DEBUG)
            if (@preg_match('#^'. $pattern .'$#', 'test') === false)
               throw new Exception (sprintf ("KValidatorElementText::regex() - Le pattern <strong>%s</strong> est invalide.", $pattern));
            elseif (false === isset ($error))
               throw new Exception ("KValidatorElementText::regex() - Vous devez renseigner une erreur à retourner.");
            
         $param = $this->_parameter("regex");
         $param->pattern = $pattern;
         $param->return  = (bool) $return;
         $param->error   = $error;
         
         return $this;
      }
      
      
      public function int ($min = null, $max = null, $inc = false) {
         $inc = (bool) $inc;

         if (true === isset($min))
            $min = filter_var ($min, FILTER_SANITIZE_NUMBER_FLOAT);

         if (true === isset($max))
            $max = filter_var ($max, FILTER_SANITIZE_NUMBER_FLOAT);

         if (KV_DEBUG && true === isset($min, $max) && (($inc && $min > $max) || (!$inc && $min >= $max)))
            throw new Exception ("KValidatorElementText::int() - Vous avez défini un interval avec un minimum supérieur à un maximum.");

         if (!$inc) {
            if (isset($min)) ++$min;
            if (isset($max)) --$max;
         }

         $param = $this->_parameter ("int");
         $param->min = $min;
         $param->max = $max;

         return $this;
      }
      
      
      public function equal ($field) {
         $field = trim ($field);
         
         if (KV_DEBUG && $field === "")
            throw new Exception ("KValidatorElementText::equal() - Vous devez renseigner le nom de l'élément à comparer.");
         
         $param = $this->_parameter ("equal");
         $param->field = $field;
         
         return $this;
      }
      
      
      public function sensitive ($value) {
         $this->sensitive = (bool) $value;
         
         return $this;
      }
      
      
      public function valid () {   
         $this->value = (string) $this->value;
         
         if (false === is_array($this->value) && true === $this->trim)
            $this->value = trim ($this->value);
         
         if ($this->required === true && empty($this->value))
            $error = "Ce champ est obligatoire !";
         else
            foreach ($this->params as &$param) {
               $error = $param->valid ($this->value, array ($this->uniqid, $this->sensitive));

               if (isset ($error))
                  break;
            }
         
         if (isset ($error)) {
            KValidator::load ($this->uniqid)->setError ($this->name, $error);
            return false;
         }
         
         KValidator::load ($this->uniqid)->setValue ($this->name, $this->value);
                 
         return true;
      }
      
      
      protected function _parameter ($param) {
         return parent::_parameter("Text", $param);
      }
   }
 
/**
 * End
 * Of
 * File
 */
?>